﻿using System;
using System.Web.Http;
using System.Web.Http.Services;
using System.Web.Mvc;

namespace Devsoft.Web.Mvc.Json
{
    public class JsonActionInvoker : ControllerActionInvoker
    {
        protected override ActionResult CreateActionResult(ControllerContext controllerContext, ActionDescriptor actionDescriptor, object actionReturnValue)
        {
            if (actionReturnValue == null)
                return new JsonResult()
                           {
                               ContentEncoding = System.Text.UTF8Encoding.UTF8,
                               JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                               Data = string.Empty
                           };
            if (actionReturnValue is ActionResult) return actionReturnValue as ActionResult;
            return new JsonResult()
                {
                    ContentEncoding = System.Text.UTF8Encoding.UTF8,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = actionReturnValue
                };
        }
    }
}